package com.gowhere.controller.basedata;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.gowhere.controller.BaseServlet;
import com.gowhere.domain.City;
import com.gowhere.domain.Product;
import com.gowhere.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;


// 匹配模式
@WebServlet("/basedata/product/*")
public class ProductServlet extends BaseServlet {

    //  添加产品
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取页面已经填写的信息并封装到Product类中,
        //    因为有日期信息,使用能处理日期格式问题的工具类
        String departureTime = req.getParameter("departureTime");
        System.out.println(departureTime);

        Product product = BeanUtil.fillBean(req,Product.class,"yyyy-MM-dd HH:mm");
        //product.setDepartureTimeStr();
        System.out.println(product);
        // 2. 业务对象使用方法执行添加操作
        productService.save(product);
        // 3. 应用场景是点击事件过后,查询数据返回遍历页面
        // req.getRequestDispatcher("basedata/product/findAll").forward(req,resp);
        findAll(req,resp);
    }

    //  分页查询
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 设置默认分页参数
        int pageNum = 1;
        int pageSize = 5;
        // 2. 获取前端页面提供的分页信息
        String pageNumStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        // 3. 进行分页信息的非空非null判断
        if (StringUtils.isNotBlank(pageNumStr)){
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }
        // 业务层调用方法执行分页查询操作
        List<Product> list = productService.findAll(pageNum,pageSize);
        // 获取的数据交给pageInfo处理
        PageInfo<Product> pageInfo = new PageInfo<Product>(list);
        // 处理的数据存入req作用域中
        req.setAttribute("page",pageInfo);
        // 跳转到产品显示页面
        req.getRequestDispatcher("/pages/basedata/product-list.jsp").forward(req,resp);
    }

    // 查找所有城市
    protected void findAllCity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 调用方法执行
        List<City> list = productService.findAllCity();
        // 将查询结果转化成json格式字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        resp.setContentType("text/json;charset=utf-8");
        // 响应回前端
        resp.getWriter().write(json);
    }

    // 根据id进行查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取需要进行编辑的数据的id
        String id = req.getParameter("id");
        // 业务层方法
        Product product = productService.findById(id);
        // 将返回的对象存入req作用域中
        req.setAttribute("product",product);
        // 跳转到修改页面
        req.getRequestDispatcher("/pages/basedata/product-update.jsp").forward(req,resp);
    }

    // 修改
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*// 获取前端请求的表单数据
        String departureTime = req.getParameter("departureTime");
        System.out.println(departureTime);
        Product product = BeanUtil.fillBean(req,Product.class,"yyyy-MM-dd HH:mm");
        // 业务层调用方法

        productService.update(product);
        // 再进行查询
        findAll(req,resp);*/
        Product product = BeanUtil.fillBean(req,Product.class,"yyyy-MM-dd HH:mm");
        String id = req.getParameter("id");
        System.out.println("=========="+id);
        productService.update(product);
        req.getRequestDispatcher("/basedata/product/findAll").forward(req,resp);
    }

    // 根据id删除
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取id字符串
        String ids = req.getParameter("ids");
        // 将字符串分开,遍历获取每一个id
        String[] split = ids.split(",");
        for (String id : split) {
            // 每获得一个id调用删除方法进行操作
            productService.deleteById(id);
        }
        // 再次遍历数据
        findAll(req,resp);
    }
    // 状态修改

    protected void change(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取传递来的status信息
        String pra = req.getParameter("product.productStatusStr");
        // 调用方法


    }
}
